home *** CD-ROM | disk | FTP | other *** search
- // findi3: (c) by Günter Nagler 1996
- #include <stdio.h>
- #include <conio.h>
- #include <string.h>
- #include <ctype.h>
- #include "korg.hpp"
- #include <dir.h>
- #include "like.h"
- #include <stdlib.h>
-
- enum KORGFILE { other, sty, arr, pcg, bsq, sng };
-
- int pause = 0;
- int linecount = 0;
-
- char* defaultmask = "*";
-
- int usage()
- {
- printf("findi3 searches names in korg files\n");
- printf("usage: findi3 [-h] [-p] [-m filemask] name ...\n");
- printf("-h or -?\t\tshow this usage information\n");
- printf("-p\t\tpause screen after each page\n");
- printf("-m filemask\tsearch only in files matching filemask (default: *.*)\n");
- printf("name can contain wildcards (*,?)\n");
- return 1;
- }
-
-
- char namebuf[4000];
-
- long namecount = 0;
- int filecount = 0;
-
- void stop()
- {
- fprintf(stderr, "program aborted by <esc>\n");
- exit(1);
- }
-
- void found(KORGFILE type, char* filename, int idx, char* s, int len)
- {
- if (namecount == 0)
- {
- printf("%-10.10s\tLoc\tFilename\n", "Name");
- printf("%-10.10s\t---\t--------\n", "--------");
- linecount += 2;
- }
- namecount++;
- linecount++;
- printf("%-10.10s\t", s);
- switch(type)
- {
- case pcg:
- if (idx < 64)
- printf("D%d%d", (idx / 8)+1, (idx %8)+1);
- else
- printf("Dr%d", idx - 64 + 7);
- break;
- case arr:
- printf("A%d%d", (idx / 8)+1, (idx %8)+1);
- break;
- case sty:
- printf("U%d", idx +1);
- break;
- case bsq:
- printf("BSQ%d", idx);
- break;
- case sng:
- printf("S%d", idx);
- break;
- default:
- printf("%d", idx);
- break;
- }
- printf("\t%s\n", filename);
- if (pause && linecount >= 24)
- {
- fprintf(stderr,"-- press any key to continue or <esc> to stop --");
- linecount = 0;
- int c = getch();
- if (c == 0)
- c = getch();
- fprintf(stderr, "\n");
- if (c == 27)
- stop();
- }
- else if (kbhit() && getch() == 27)
- stop();
- }
-
- int findi3(KORGFILE type, char* filename, char* namebuf, int namebuflen, int namelen, int argc, char** argv)
- {
- int ret = 0;
- char pattern[256];
-
- for (int arg = 0; arg < argc; arg++)
- if (argv[arg])
- {
- strcpy(pattern, argv[arg]);
- int patternlen = strlen(pattern);
- if (patternlen == 0)
- continue;
- strcat(pattern, "*"); // prefix search
- patternlen++;
- for (int i = 0, idx = 0; i < namebuflen; i+= namelen, idx++)
- {
- int len = namelen;
- while (len > 0 && namebuf[i+len-1] == ' ')
- len--;
- if (len > 0 && like(namebuf+i, len, pattern, patternlen))
- {
- found(type, filename, idx, namebuf+i, len);
- ret = 1;
- }
- }
- }
- return ret;
- }
-
- int findi3(KORGFILE type, char* filename, FILE* f, int argc, char** argv)
- {
- block names;
- int namebuflen, i, idx;
-
- if (!f)
- return 0;
-
- int namelen = 10;
- switch(type)
- {
- case pcg:
- fseek(f, 0x18, SEEK_SET);
- if (fread(&names, sizeof(names), 1, f) != 1)
- return 0;
- break;
- case arr:
- case sty:
- fseek(f, 0x10, SEEK_SET);
- if (fread(&names, sizeof(names), 1, f) != 1)
- return 0;
- break;
- case bsq:
- fseek(f, 0x10, SEEK_SET);
- if (fread(namebuf, 2292, 1, f) != 1)
- return 0;
- namebuflen = 0;
- for (i = 0, idx = 0; idx < 10; i+= 0xc3, idx++)
- {
- if (i != namebuflen)
- strncpy(namebuf+namebuflen, namebuf+i, namelen);
- namebuflen += namelen;
- }
- return findi3(type, filename, namebuf, namebuflen, namelen, argc, argv);
- case sng:
- fseek(f, 0x10, SEEK_SET);
- if (fread(namebuf, 2960, 1, f) != 1)
- return 0;
- namebuflen = 0;
- for (i = 0x35, idx = 0; idx < 10; i+= 0x128, idx++)
- {
- if (i != namebuflen)
- strncpy(namebuf+namebuflen, namebuf+i, namelen);
- namebuflen += namelen;
- }
- return findi3(type, filename, namebuf, namebuflen, namelen, argc, argv);
- default:
- return 0;
- }
- fseek(f, names.adr, SEEK_SET);
- if (ftell(f) != names.adr || names.len <= 0)
- return 0;
- if ((names.len % namelen) != 0)
- return 0;
- if (names.len > sizeof(namebuf))
- return 0;
- if (fread(namebuf, namebuflen=(int)names.len, 1, f) != 1)
- return 0;
- return findi3(type, filename, namebuf, namebuflen, namelen, argc, argv);
- }
-
- int findi3(char* filemask, int argc, char** argv)
- {
- struct ffblk ff;
- int ret = 0;
- FILE* f = 0;
- KORGFILE type;
- static char path[128];
- char* filename;
-
- strcpy(path, filemask);
- filename = strrchr(path, '\\');
- if (filename)
- filename++;
- else
- {
- filename = strchr(path, ':');
- if (filename)
- filename++;
- else
- filename = path;
- }
- int done = findfirst(path, &ff, 0);
- while (!done)
- {
- char* ext = strrchr(ff.ff_name, '.');
- if (!ext)
- type = other;
- else
- {
- ext++;
- if (stricmp(ext, "PCG") == 0)
- type = pcg;
- else if (stricmp(ext, "STY") == 0)
- type = sty;
- else if (stricmp(ext, "ARR") == 0)
- type = arr;
- else if (stricmp(ext, "BSQ") == 0)
- type = bsq;
- else if (stricmp(ext, "SNG") == 0)
- type = sng;
- else
- type = other;
- }
-
- if (type == other)
- f = 0;
- else if (type == bsq && ff.ff_fsize <= 2308)
- {
- filecount++;
- f = 0; // empty bsq
- }
- else if (type == sng && ff.ff_fsize <= 3718)
- {
- filecount++;
- f = 0; // empty sng
- }
- else
- {
- strcpy(filename, ff.ff_name);
- f = fopen(path, "rb");
- }
- if (f)
- {
- char id[5];
-
- if (fread(id, sizeof(id), 1, f) == 1 && strncmp(id, "KORG9", 5) == 0)
- {
- filecount++;
-
- int r = findi3(type, path, f, argc, argv);
- if (r)
- ret = 1;
- }
- fclose(f);
- }
- done = findnext(&ff);
- }
- return ret;
- }
-
- int main(int argc, char** argv)
- {
- char* filemask = "*.*";
-
- argc--; argv++;
-
- while (argc > 0 && (**argv == '-' || **argv == '/'))
- {
- char* option = (*argv)+1;
- if (strnicmp(option, "help", 1) == 0 || strcmp(option,"?") == 0)
- {
- usage();
- return 1;
- }
- if (strnicmp(option, "mask", 1) == 0 || strnicmp(option, "file", 1) == 0)
- {
- argc--; argv++;
- if (argc == 0)
- fprintf(stderr, "option -m needs a filemask parameter (default: *.*)\n");
- else
- {
- filemask = *argv++; argc--;
- }
- continue;
- }
- if (strnicmp(option, "pause", 1) == 0)
- {
- pause = 1;
- argc--; argv++; continue;
- }
- fprintf(stderr, "invalid option %s\n", *argv);
- argc--; argv++;
- }
-
- if (argc == 0)
- findi3(filemask, 1, &defaultmask);
- else
- findi3(filemask, argc, argv);
- if (filecount == 0)
- printf("No KORG files found\n");
- else if (namecount == 0)
- printf("No matching names found\n");
- return 0;
- }
-